package atom.core3.spring.util;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringContext {

	private static SpringContext instance = new SpringContext();
	private static ServletContext servletContext = null;
	public static SpringContext getInstance() {
		return instance;
	}
	
	public ApplicationContext initWebContext() {
		context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(servletContext);
		
		return context;
	}
	public ApplicationContext initWebContext2(ServletContext sc) {
		servletContext = sc;
		context = WebApplicationContextUtils.getWebApplicationContext(sc);
		return context;
	}	
	public ApplicationContext initWebContext(ServletContext sc) {
		servletContext = sc;
		context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(sc);
		return context;
	}
	public ApplicationContext initFilesContext(String[] locations) {
		context = new FileSystemXmlApplicationContext(locations);		
		return context;
	}

	private ApplicationContext context;

	private static String defaultContextFile = "applicationContext.xml";

	public ApplicationContext getContext() {
		return context;
	}

	public ApplicationContext initContext() {
		context = new ClassPathXmlApplicationContext(defaultContextFile);
		return context;
	}

	public ApplicationContext initContext(String classPathFile) {
		context = new ClassPathXmlApplicationContext(classPathFile);
		return context;
	}

	public ApplicationContext initContext(String classPathFiles[]) {
		context = new ClassPathXmlApplicationContext(classPathFiles);
		return context;
	}

	public static Object getBean(String serviceName) {
		ApplicationContext context = instance.getContext();
		Object bean = null;
		bean = context.getBean(serviceName);
		try {
			//bean = context.getBean(serviceName);
		} catch (Exception e) {
			e.printStackTrace();
			
			/*
			//无法刷新 是个bug
			if (context instanceof AbstractRefreshableConfigApplicationContext) {
				((AbstractRefreshableConfigApplicationContext) context).refresh();
				bean = context.getBean(serviceName);
			}
			*/
			//重载机制 会导致服务线程的dao失效 java.sql.SQLException: Data source is closed
			/*
			if (context.getParent() != null) {
			    ((AbstractRefreshableApplicationContext) context.getParent())
			            .refresh();
			}			
			((AbstractRefreshableApplicationContext) context).refresh();	
			bean = context.getBean(serviceName);
			*/
			
		}
		
		return bean;
	}

	public static DefaultListableBeanFactory getBeanFactory() {
		ApplicationContext context = instance.getContext();
		
		return (DefaultListableBeanFactory) ((AbstractApplicationContext) context).getBeanFactory();
	}
	
	public static Object getSingleton(String beanName) {
		return getBeanFactory().getSingleton(beanName);
	}
}
